package com.ruoyi.project.cms.service.impl;

import java.util.Collection;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.redis.RedisCache;
import jdk.nashorn.internal.runtime.GlobalConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.cms.mapper.WxSubscribeMsgMapper;
import com.ruoyi.project.cms.domain.WxSubscribeMsg;
import com.ruoyi.project.cms.service.IWxSubscribeMsgService;
import org.springframework.util.CollectionUtils;

/**
 * 小程序订阅消息模板维护Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-09-10
 */
@Service
public class WxSubscribeMsgServiceImpl implements IWxSubscribeMsgService 
{
    @Autowired
    private WxSubscribeMsgMapper wxSubscribeMsgMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询小程序订阅消息模板维护
     * 
     * @param id 小程序订阅消息模板维护ID
     * @return 小程序订阅消息模板维护
     */
    @Override
    public WxSubscribeMsg selectWxSubscribeMsgById(Long id)
    {
        return wxSubscribeMsgMapper.selectWxSubscribeMsgById(id);
    }

    /**
     * 查询小程序订阅消息模板维护列表
     * 
     * @param wxSubscribeMsg 小程序订阅消息模板维护
     * @return 小程序订阅消息模板维护
     */
    @Override
    public List<WxSubscribeMsg> selectWxSubscribeMsgList(WxSubscribeMsg wxSubscribeMsg)
    {
        return wxSubscribeMsgMapper.selectWxSubscribeMsgList(wxSubscribeMsg);
    }

    /**
     * 新增小程序订阅消息模板维护
     * 
     * @param wxSubscribeMsg 小程序订阅消息模板维护
     * @return 结果
     */
    @Override
    public int insertWxSubscribeMsg(WxSubscribeMsg wxSubscribeMsg)
    {
        //新增删redis相关数据
        if(ObjectUtil.isNotEmpty(wxSubscribeMsg.getType())){//删除相关集合
            redisCache.deleteObject(StrUtil.format(Constants.WX_SUBSCRIBE_MSG_KEY,wxSubscribeMsg.getType()));
        }
        return wxSubscribeMsgMapper.insertWxSubscribeMsg(wxSubscribeMsg);
    }

    /**
     * 修改小程序订阅消息模板维护
     * 
     * @param wxSubscribeMsg 小程序订阅消息模板维护
     * @return 结果
     */
    @Override
    public int updateWxSubscribeMsg(WxSubscribeMsg wxSubscribeMsg)
    {
        //内容修改 删除redis对应相关数据
        if(ObjectUtil.isNotEmpty(wxSubscribeMsg.getCode())){//删除相关单项
          redisCache.deleteObject(wxSubscribeMsg.getCode());
        }
        if(ObjectUtil.isNotEmpty(wxSubscribeMsg.getType())){//删除相关集合
          redisCache.deleteObject(StrUtil.format(Constants.WX_SUBSCRIBE_MSG_KEY,wxSubscribeMsg.getType()));
        }

        wxSubscribeMsg.setUpdateTime(DateUtils.getNowDate());
        return wxSubscribeMsgMapper.updateWxSubscribeMsg(wxSubscribeMsg);
    }

    /**
     * 批量删除小程序订阅消息模板维护
     * 
     * @param ids 需要删除的小程序订阅消息模板维护ID
     * @return 结果
     */
    @Override
    public int deleteWxSubscribeMsgByIds(Long[] ids)
    {
        return wxSubscribeMsgMapper.deleteWxSubscribeMsgByIds(ids);
    }

    /**
     * 删除小程序订阅消息模板维护信息
     * 
     * @param id 小程序订阅消息模板维护ID
     * @return 结果
     */
    @Override
    public int deleteWxSubscribeMsgById(Long id)
    {
        return wxSubscribeMsgMapper.deleteWxSubscribeMsgById(id);
    }

    @Override
    public WxSubscribeMsg selectByCode(String code) {
        WxSubscribeMsg wsm = null;

//        wsm = redisCache.getCacheObject(code);
//        if(ObjectUtil.isNotEmpty(wsm)){
//            return wsm;
//        }
        wsm = wxSubscribeMsgMapper.selectByCode(code);
        if(ObjectUtil.isNotEmpty(wsm)){
            redisCache.setCacheObject(code,wsm);
        }
        return wsm;
    }

    @Override
    public List<String> selectByType(Integer type) {
        String key = StrUtil.format(Constants.WX_SUBSCRIBE_MSG_KEY,type);
        List<String> list = redisCache.getCacheList(key);
        if(CollectionUtils.isEmpty(list)){
            WxSubscribeMsg wsm = new WxSubscribeMsg();
            wsm.setType(type);
           List<WxSubscribeMsg> wxSubscribeMsgList = wxSubscribeMsgMapper.selectWxSubscribeMsgList(wsm);
           for (WxSubscribeMsg w :wxSubscribeMsgList){
               list.add(w.getTemplateId());
           }
           if(!CollectionUtils.isEmpty(list)){
               redisCache.setCacheList(key,list);
           }
        }
        return list;
    }
}
